<?php
/**
 * Database class
 * @author Joe Minichino <joe@dsforge.net>
 * 
 */
class DB {

    private static $instance;

    private $db;
    private function  __construct() {

    }

    public static function get(){
        if(self::$instance==null){
            $array = parse_ini_file("config/config.ini",true);
            $init = $array['MySQL'];

            self::$instance = new DB();
            self::$instance->db = new mysqli($init['host'],$init['username'],$init['password'],$init['dbname']);
            if(mysqli_connect_error() ){
                echo "error: " . mysqli_connect_error();
            }
        }
        return self::$instance;
    }

    public function getDb(){
        return self::$instance->db;
    }

    public function save(&$object){
        $this->db->query($object->getInsertQuery());
        if($this->db->errno){
            Debug::log("ERROR: ".$this->db->error);
            return $this->db->error;
        }
        $object = $this->getObject(get_class($object), $this->db->insert_id);
        return "ok";
    }

    public function update(&$object){
        $this->db->query($object->getUpdateQuery());
        if($this->db->errno){
            Debug::log("ERROR: " .$this->db->error);
            return $this->db->error;
        }
        $object = $this->getObject(get_class($object), $object->id);
        return "ok";
    }

    public function delete($object){
        $this->db->query($object->getDeleteQuery());
        if($this->db->errno){
            Debug::log($this->db->error);
        }
    }
    
    public function query($query){
        $result = $this->db->query($query);
        if($this->db->error){
            Debug::log($this->db->error);
            return null;
        } else {
            
            $retval = array();
            while($row = $result->fetch_array(MYSQLI_BOTH)){
                $retval[] = $row;
            }
            return $retval;
        }
    }

    public function getObject($class, $value, $param="id"){
        $obj = new $class();
        $result = $this->db->query($obj->getQuery($value,$param));
        if($this->db->errno){
            Debug::log($this->db->error);
        }
        if($result==null) return null;
        $retval = null;
        while($o = $result->fetch_object($class)){
            $retval = $o;
        }
        return $retval;
    }

    public function getList($class,$offset=0,$limit=null){
        $obj = new $class();
        $result = $this->db->query($obj->getQuery(0,"id",">",$offset,$limit));
        if($this->db->errno){
            Debug::log($this->db->error);
        }
        $results = array();
        if($result==null) return null;
        while($o = $result->fetch_object($class)){
            $results[] = $o;
        }
        return $results;
    }

    public function getWhere($class,$whereClause, $offset=0,$limit=null){
        $obj = new $class();
        $result = $this->db->query($obj->getWhereQuery($whereClause,$offset,$limit));
        if($this->db->errno){
            Debug::log($this->db->error);
        }
        if($result==null) return null;
        $results = array();
        while($o = $result->fetch_object($class)){
            $results[] = $o;
        }
        return $results;
        
    }

    public function getWithQuery($class,$sql){
        $obj = new $class();
        $result = $this->db->query($sql);
        if($this->db->errno){
            Debug::log($this->db->error);
        }
        if($result==null) return null;
        $results = array();
        while($o = $result->fetch_object($class)){
            //$results[$o->id] = $o;
            $results[] = $o;
        }
        return $results;
    }

}
